package com.admin.action;

import com.admin.service.LogoutService;
import com.data.bean.Account;
import com.data.util.CheckDataUtils;
import com.data.util.Md5CodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

/**
 * 退出登录，修改密码等相关操作
 *
 * @author:HingLo
 * @create 2017-08-25 10:20
 **/
@Controller
@RequestMapping("/logoutAction/admin")
public class LogoutAction {

    @Autowired
    private LogoutService logoutService;

    @RequestMapping("/logout")
    public String logoutAction(HttpServletRequest request) {
        Account account = (Account) request.getSession().getAttribute("Account");
        if (account != null) {
            try {
                request.getSession().removeAttribute("Account");
                return "index";

            } catch (Exception e) {
                return "404";
            }


        }
        return "login";
    }


    /**
     * 修改密码
     *
     * @param password
     * @param newPassword
     * @param request
     * @return
     */
    @RequestMapping(value = "changePassword")
    public String changePassword(String password, String newPassword, HttpServletRequest request) {
        Account account = (Account) request.getSession().getAttribute("Account");
        //数据校验是否正确
        if (CheckDataUtils.stringUtils(password, newPassword) && account != null) {
            password = Md5CodeUtils.md5(password.trim());
            if (password.equals(account.getPassword())) {
                account.setPassword(Md5CodeUtils.md5(newPassword));
                boolean bool = logoutService.changePasswordService(account);
                //删除回话
                request.getSession().removeAttribute("Account");
                request.setAttribute("bool", bool);
                return "/WEB-INF/admin/password";
            }
        }

        return "/WEB-INF/admin/password";
    }

    @RequestMapping(value = "inputchangePassword", method = RequestMethod.GET)
    public String inputChangePassword() {

        return "/WEB-INF/admin/password";
    }


}
